<template>
  <el-dialog
      top="5vh"
      width="1200px"
      title="添加/编辑旅行订单"
      :visible.sync="dialogValue"
      :close-on-click-modal="false"
      append-to-body
  >
    <SubmitForm
        :rule-form="ruleForm"
        :rules="rules"
        :form-list="formList"
        @change="handleFormChange"
        @save="onSave"
        @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import {http} from '@/api'
import { ORDER_STATUS, TRAVEL_ORDER_STATUS_TEXT } from '@/utils/enum'

export default {
  name: 'TravelOrderAdd',
  data() {
    return {
      apiKey: 'travelOrder',
      dialogValue: false,
      ruleForm: {},
      defaultForm: {
        order_no: '',
        travel_id: 0,
        travel_sku_id: 0,
        travel_sku_stock_id: 0,
        travel_name: '',
        travel_cover: '',
        sku_spec: '',
        travel_date: '',
        user_id: 0,
        adult_count: 0,
        child_count: 0,
        passenger_list: '',
        contact_name: '',
        contact_phone: '',
        remark: '',
        room_count: 0,
        single_price_diff: 0.00,
        single_room_diff_amount: 0.00,
        status: 'pending',
        time_expire: null,
        count: 1,
        amount: 0.00,
        prepay_amount: 0.00,
        payment_time: null,
        payment_type: '',
        payment_amount: 0.00,
        hotel_level_name: '',
        hotel_level_price: 0.00,
        hotel_level_count: 0,
        hotel_level_amount: 0.00,
        transaction_id: '',
        prepay_id: '',
        refund_no: '',
        refund_transaction_id: '',
        refund_status: -1,
        refund_time: null,
        refund_success_time: null,
        refund_reason: '',
        refund_remark: '',
        refund_amount: 0.00,
        cancel_time: null
      },
      rules: {
        order_no: [{required: true, message: '订单号不能为空'}],
        travel_id: [{required: true, message: '旅游产品ID不能为空'}],
        travel_name: [{required: true, message: '旅游产品名称不能为空'}],
        travel_date: [{required: true, message: '旅游日期不能为空'}],
        user_id: [{required: true, message: '用户ID不能为空'}],
        contact_name: [{required: true, message: '联系人姓名不能为空'}],
        contact_phone: [{required: true, message: '联系电话不能为空'}],
        status: [{required: true, message: '订单状态不能为空'}],
      },
      formList: [
        {
          label: '订单号',
          prop: 'order_no',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入订单号'
        },
        {
          label: '旅游产品ID',
          prop: 'travel_id',
          name: 'number',
          colSpan: 12,
          min: 1,
          placeholder: '请输入旅游产品ID'
        },
        {
          label: '旅游产品名称',
          prop: 'travel_name',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入旅游产品名称'
        },
        {
          label: '旅游产品封面',
          prop: 'travel_cover',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入旅游产品封面URL'
        },
        {
          label: '旅游日期',
          prop: 'travel_date',
          name: 'date',
          colSpan: 12,
          placeholder: '请选择旅游日期',
          valueFormat: 'YYYY-MM-DD'
        },
        {
          label: '用户ID',
          prop: 'user_id',
          name: 'number',
          colSpan: 12,
          min: 1,
          placeholder: '请输入用户ID'
        },
        {
          label: '联系人姓名',
          prop: 'contact_name',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入联系人姓名'
        },
        {
          label: '联系电话',
          prop: 'contact_phone',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入联系电话'
        },
        {
          label: '成人数量',
          prop: 'adult_count',
          name: 'number',
          colSpan: 8,
          min: 0,
          placeholder: '请输入成人数量'
        },
        {
          label: '儿童数量',
          prop: 'child_count',
          name: 'number',
          colSpan: 8,
          min: 0,
          placeholder: '请输入儿童数量'
        },
        {
          label: '房间数量',
          prop: 'room_count',
          name: 'number',
          colSpan: 8,
          min: 0,
          placeholder: '请输入房间数量'
        },
        {
          label: '人员列表',
          prop: 'passenger_list',
          name: 'textarea',
          colSpan: 24,
          rows: 4,
          placeholder: '请输入人员列表（JSON格式）：[{"name":"张三","id_card":"123456789012345678","phone":"13800138000","type":"adult"}]'
        },
        {
          label: '订单备注',
          prop: 'remark',
          name: 'textarea',
          colSpan: 24,
          rows: 2,
          placeholder: '请输入订单备注'
        },
        {
          label: '订单状态',
          prop: 'status',
          name: 'select',
          colSpan: 12,
          placeholder: '请选择订单状态',
          options: [
            { value: ORDER_STATUS.PENDING, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.PENDING] },
            { value: ORDER_STATUS.PAID, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.PAID] },
            { value: ORDER_STATUS.SHIPPED, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.SHIPPED] },
            { value: ORDER_STATUS.COMPLETED, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.COMPLETED] },
            { value: ORDER_STATUS.CANCELLED, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.CANCELLED] },
            { value: ORDER_STATUS.REFUNDING, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.REFUNDING] },
            { value: ORDER_STATUS.REFUNDED, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.REFUNDED] },
            { value: ORDER_STATUS.REFUND_FAILED, label: TRAVEL_ORDER_STATUS_TEXT[ORDER_STATUS.REFUND_FAILED] }
          ]
        },
        {
          label: '商品数量',
          prop: 'count',
          name: 'number',
          colSpan: 8,
          min: 1,
          placeholder: '请输入商品数量'
        },
        {
          label: '订单金额',
          prop: 'amount',
          name: 'number',
          colSpan: 8,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入订单金额'
        },
        {
          label: '预付金额',
          prop: 'prepay_amount',
          name: 'number',
          colSpan: 8,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入预付金额'
        },
        {
          label: '支付金额',
          prop: 'payment_amount',
          name: 'number',
          colSpan: 8,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入支付金额'
        },
        {
          label: '支付方式',
          prop: 'payment_type',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入支付方式'
        },
        {
          label: '支付时间',
          prop: 'payment_time',
          name: 'datetime',
          colSpan: 12,
          dateType: 'datetime',
          valueFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        {
          label: '酒店级别名称',
          prop: 'hotel_level_name',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入酒店级别名称'
        },
        {
          label: '酒店级别价格',
          prop: 'hotel_level_price',
          name: 'number',
          colSpan: 12,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入酒店级别价格'
        },
        {
          label: '酒店级别数量',
          prop: 'hotel_level_count',
          name: 'number',
          colSpan: 8,
          min: 0,
          placeholder: '请输入酒店级别数量'
        },
        {
          label: '酒店级别总价',
          prop: 'hotel_level_amount',
          name: 'number',
          colSpan: 8,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入酒店级别总价'
        },
        {
          label: '单房差价格',
          prop: 'single_price_diff',
          name: 'number',
          colSpan: 8,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入单房差价格'
        },
        {
          label: '单房差总金额',
          prop: 'single_room_diff_amount',
          name: 'number',
          colSpan: 8,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入单房差总金额'
        },
        {
          label: '订单过期时间',
          prop: 'time_expire',
          name: 'datetime',
          colSpan: 12,
          dateType: 'datetime',
          valueFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        {
          label: '取消时间',
          prop: 'cancel_time',
          name: 'datetime',
          colSpan: 12,
          dateType: 'datetime',
          valueFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        {
          label: '退款状态',
          prop: 'refund_status',
          name: 'select',
          colSpan: 12,
          placeholder: '请选择退款状态',
          options: [
            { value: -1, label: '未退款' },
            { value: 0, label: '退款中' },
            { value: 1, label: '退款成功' },
            { value: 2, label: '退款失败' }
          ]
        },
        {
          label: '退款金额',
          prop: 'refund_amount',
          name: 'number',
          colSpan: 12,
          min: 0,
          precision: 2,
          step: 0.01,
          placeholder: '请输入退款金额'
        },
        {
          label: '退款原因',
          prop: 'refund_reason',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入退款原因'
        },
        {
          label: '退款备注',
          prop: 'refund_remark',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入退款备注'
        },
        {
          label: '申请退款时间',
          prop: 'refund_time',
          name: 'datetime',
          colSpan: 12,
          dateType: 'datetime',
          valueFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        {
          label: '退款成功时间',
          prop: 'refund_success_time',
          name: 'datetime',
          colSpan: 12,
          dateType: 'datetime',
          valueFormat: 'YYYY-MM-DD HH:mm:ss'
        },
        {
          label: '微信支付交易号',
          prop: 'transaction_id',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入微信支付交易号'
        },
        {
          label: '微信预支付ID',
          prop: 'prepay_id',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入微信预支付ID'
        },
        {
          label: '退款单号',
          prop: 'refund_no',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入退款单号'
        },
        {
          label: '退款交易号',
          prop: 'refund_transaction_id',
          name: 'input',
          colSpan: 12,
          placeholder: '请输入退款交易号'
        },
      ]
    }
  },
  methods: {
    handleFormChange({key, val}) {
      // 处理表单变化
    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...this.defaultForm,
          order_no: this.generateOrderNo()
        }
      }
    },
    generateOrderNo() {
      // 生成订单号：年月日时分秒+4位随机数
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0');
      const day = String(now.getDate()).padStart(2, '0');
      const hour = String(now.getHours()).padStart(2, '0');
      const minute = String(now.getMinutes()).padStart(2, '0');
      const second = String(now.getSeconds()).padStart(2, '0');
      const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
      return `${year}${month}${day}${hour}${minute}${second}${random}`;
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, this.ruleForm).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style> 